<?php 
namespace app\api\controller;

use think\Controller;
use think\facade\Validate;

/**
 * @title 登录接口
 */
class Login extends Controller
{
	/**
	 * @title 获取验证码
	 * @description 获取验证码
	 * @url /api/get_captcha
	 * @method post
	 *
	 * @param name:phone type:string require:1 desc:手机号
	 * 
	 */
	public function get_captcha()
	{
		$data = [
			'phone' => input('phone')
		];

		$rule = [
			'phone' => 'require|mobile'
		];

		$msg = [
			'phone.require' => '请输入手机号',
			'phone.mobile' => '手机格式不正确'
		];

		$validate = Validate::make($rule,$msg);
		if(!$validate->check($data)){
			$this->result('',0,$validate->getError(),'json');
		}

		$captcha = rand(100000,999999);
		cache('captcha'.$data['phone'],$captcha,60*5);

		$content = '您的验证码是：' . $captcha . '。请不要把验证码泄露给其他人。';

        // $url = "http://106.ihuyi.com/webservice/sms.php?method=Submit&account=C61835695&password=c7d8f02419a0cc58d4b5c275ae0779a1&mobile=" . $phone . "&content=" . $content;
        // $data = file_get_contents($url);
        // $xml = simplexml_load_string($data);

		$this->result('',1,'发送成功,验证码为：'.$captcha,'json');

	}

	/**
	 * @title 注册
	 * @description 注册
	 * @url /api/register
	 * @method post
	 *
	 * @param name:phone type:string require:1 desc:手机号
	 * @param name:captcha type:string require:1 desc:验证码
	 * @param name:password type:string require:1 desc:密码
	 */
	public function register()
	{
		$data = [
			'phone' => trim(input('phone')),
			'captcha' => trim(input('captcha')),
			'password' => trim(input('password'))
		];

		$rule = [
			'phone' => 'require|mobile',
			'captcha' => 'require',
			'password' => 'require|alphaNum|length:6,12'
		];

		$msg = [
			'phone.require' => '请输入手机号',
			'phone.mobile' => '手机号格式不正确',
			'captcha.require' => '请输入验证码',
			'password.require' => '请输入密码',
			'password.alphaNum' => '密码为6-12位数字和字母的组合',
			'password.length' => '密码为6-12位数字和字母的组合'
		];

		$validate = Validate::make($rule,$msg);
		if(!$validate){
			$this->result('',0,$validate->getError(),'json');
		}

		//判断手机号是否已经注册过
		$find = model('User')::getByPhone($data['phone']);
		if($find){
			$this->result('',0,'此手机号已经被注册','json');
		}
		//验证验证码
		if($data['captcha'] != cache('captcha'.$data['phone'])){
			$this->result('',0,'验证码不正确','json');
		}else{
			cache('captcha'.$data['phone'],null,0);
		}

		$register_data = [
			'phone' => $data['phone'],
			'password' => md5($data['password']),
			'username' => 'YL'.hywx_random(6,1)
		];

		$res = model('User')->create($register_data);
		if($res){
			$this->result(['user_id'=>$res->id],1,'注册成功','json');
		}else{
			$this->result('',0,'注册失败，请稍后重试','json');
		}

	}

	/**
	 * @title 验证码登录
	 * @description 验证码登录
	 * @url /api/captcha_login
	 * @method post
	 *
	 * @param name:phone type:string require:1 desc:手机号
	 * @param name:captcha type:string require:1 desc:验证码
	 */
	public function captcha_login()
	{
		$data = [
			'phone' => trim(input('phone')),
			'captcha' => trim(input('captcha'))
		];

		$rule = [
			'phone' => 'require|mobile',
			'captcha' => 'require'
		];

		$msg = [
			'phone.require' => '请输入手机号',
			'phone.mobile' => '手机号格式不正确',
			'captcha.require' => '请输入验证码'
		];

		$validate = Validate::make($rule,$msg);
		if(!$validate){
			$this->result('',0,$validate->getError(),'json');
		}

		//判断账号是否存在
		$find = model('User')::getByPhone($data['phone']);
		if(!$find){
			$this->result('',0,'此账号不存在，请先去注册','json');
		}
		//验证验证码
		if($data['captcha'] != cache('captcha'.$data['phone'])){
			$this->result('',0,'验证码不正确','json');
		}else{
			cache('captcha'.$data['phone'],null,0);
		}

		//保存cookie
		cookie('user_id',$find->id,180*24*60*60);

		$this->result(['user_id'=>$find->id,'phone'=>$find->phone,'is_real'=>$find->is_real],1,'登录成功','json');
	}

	/**
	 * @title 密码登录
	 * @description 密码登录
	 * @url /api/password_login
	 * @method post
	 *
	 * @param name:phone type:string require:1 desc:手机号
	 * @param name:password type:string require:1 desc:密码
	 */
	public function password_login()
	{
		$data = [
			'phone' => trim(input('phone')),
			'password' => trim(input('password'))
		];

		$rule = [
			'phone' => 'require|mobile',
			'password' => 'require|alphaNum|length:6,12'
		];

		$msg = [
			'phone.require' => '请输入手机号',
			'phone.mobile' => '手机号格式不正确',
			'password.require' => '请输入密码',
			'password.alphaNum' => '密码为6-12位数字和字母的组合',
			'password.length' => '密码为6-12位数字和字母的组合'
		];

		$validate = Validate::make($rule,$msg);
		if(!$validate){
			$this->result('',0,$validate->getError(),'json');
		}

		//判断账号是否存在
		$find = model('User')::getByPhone($data['phone']);
		if(!$find){
			$this->result('',0,'此账号不存在，请先去注册','json');
		}
		//判断密码是否正确
		if($find['password'] != md5($data['password'])){
			$this->result('',0,'密码不正确','json');
		}

		//保存cookie
		cookie('user_id',$find->id,180*24*60*60);

		$this->result(['user_id'=>$find->id,'phone'=>$find->phone,'is_real'=>$find->is_real],1,'登录成功','json');
	}

	/**
	 * @title 退出
	 * @description 退出
	 * @url /api/loginout
	 * @method post
	 *
	 */
	public function loginout()
	{
		cookie('user_id',null);

		$this->result('',1,'退出成功','json');
	}
	
}
 ?>